﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using ProxyViewModel;
using System.ComponentModel;
namespace AutoViewModel.Tests {
	[TestClass]
	public class PropertyInterceptionTest {
		public class AViewModel {
			public virtual string Property1 {
				get;
				set;
			}

			public virtual string Property2 {
				get;
				set;
			}

			public virtual decimal Property3 {
				get;
				set;
			}

			public bool NotifyPredicateProperty1(string newvalue, string oldvalue) {
				return newvalue != "catch";
			}

			public bool NotifyPredicateProperty2(string newvalue, string oldvalue) {
				return newvalue != "catch2";
			}

			public bool NotifyPredicateProperty3(decimal newvalue, decimal oldvalue) {
				return decimal.Parse(newvalue.ToString()) != 356;
			}
		}

		public class BViewModel : AViewModel
		{
			public virtual int Property4 {
				get;
				set;
			}

			public bool NotifyPredicateProperty4(int newvalue, int oldvalue) {
				return newvalue != 5;
			}
		}

		[TestMethod]
		public void TestNotifyPropertyPredicate(){
			AViewModel myViewModel = ProxyViewModelFactory.CreateProxyViewModel<AViewModel>();

			int notifyPropertyCallNumber = 0;

			((INotifyPropertyChanged)myViewModel).PropertyChanged += (e, s) => {
				notifyPropertyCallNumber++;
			};

			myViewModel.Property1 = "catch";
			myViewModel.Property2 = "catch2";
			myViewModel.Property3 = 356;

			myViewModel.Property1 = "myvalue1";
			myViewModel.Property2 = "myvalue2";
			myViewModel.Property3 = 555;

			Assert.AreEqual(notifyPropertyCallNumber, 3, "PropertyChanged must be called only 3 times here.");
		}

		[TestMethod]
		public void TestNotifyPropertyPredicateWithInheritance() {
			BViewModel myViewModel = ProxyViewModelFactory.CreateProxyViewModel<BViewModel>();

			int notifyPropertyCallNumber = 0;

			((INotifyPropertyChanged)myViewModel).PropertyChanged += (e, s) => {
				notifyPropertyCallNumber++;
			};

			myViewModel.Property1 = "catch";
			myViewModel.Property2 = "catch2";
			myViewModel.Property3 = 356;
			myViewModel.Property4 = 5;

			myViewModel.Property1 = "myvalue1";
			myViewModel.Property2 = "myvalue2";
			myViewModel.Property3 = 555;
			myViewModel.Property4 = 34;

			myViewModel.Property4 = 5;

			Assert.AreEqual(notifyPropertyCallNumber, 4, "PropertyChanged must be called only 4 times here.");
		}
	}
}
